package com.tcd.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tcd.helper.redis.RedisHelper;
import com.tcd.helper.redis.ZSetRedisHelper;
import com.tcd.mapper.UserGroupMapper;
import com.tcd.pojo.UserGroup;
import com.tcd.service.UserGroupService;
import com.tcd.utils.AccountUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 用户组 服务实现类
 * </p>
 *
 * @author space
 * @since 2021-05-25
 */
@Service
public class UserGroupServiceImpl extends ServiceImpl<UserGroupMapper, UserGroup> implements UserGroupService {
    @Autowired(required = false)
    UserGroupMapper groupMapper;

    private static final String REDIS_VAL_NAME = "userGroupSet";
    private static final String REDIS_VAL_KEY = "groupId";

    @Override
    public Integer addUserGroup(UserGroup group) {
        int row = groupMapper.insert(group);
        if (row > 0){
            ZSetRedisHelper.addSet(REDIS_VAL_NAME,REDIS_VAL_KEY,group);
        }
        return row;
    }

    @Override
    public Integer updateUserGroup(UserGroup group) {
        int row = groupMapper.updateById(group);
        if(row > 0){
            ZSetRedisHelper.updateSet(REDIS_VAL_NAME,REDIS_VAL_KEY,group);
        }
        return row;
    }

    @Override
    public Integer deleteUserGroup(Integer[] ids) {
        int row = groupMapper.deleteBatchIds(Arrays.asList(ids));
        if (row > 0){
            ZSetRedisHelper.deleteSet(REDIS_VAL_NAME,ids);
        }
        return row;
    }

    @Override
    public List<UserGroup> getUserGroupFilter(Integer pageIndex, Integer pageSize, String name, Boolean state, Integer roleId) {
        if (!RedisHelper.hasKey(REDIS_VAL_NAME)) {
            List<UserGroup> list = groupMapper.selectUserGroupFilter(1, 0, null, null, null);
            ZSetRedisHelper.addSet(REDIS_VAL_NAME, REDIS_VAL_KEY, list, true);
        }
        if(AccountUtils.isNull(name) && AccountUtils.isAllNull(state,roleId)){
            return ZSetRedisHelper.getValSet(REDIS_VAL_NAME,UserGroup.class,(pageIndex-1)*pageSize,pageSize);
        }else {
            return groupMapper.selectUserGroupFilter(pageIndex,pageSize,name,state,roleId);
        }
    }

    @Override
    public Integer getUserGroupFilterCount(String name, Boolean state, Integer roleId) {
        if(AccountUtils.isNull(name) && AccountUtils.isAllNull(state,roleId)){
            return ZSetRedisHelper.getValCountSet(REDIS_VAL_NAME);
        }else {
            return groupMapper.selectUserGroupFilterCount(name,state,roleId);
        }
    }

    @Override
    public Integer setUserGroupRole(Integer groupId, Integer[] roleIds) {
        int row = groupMapper.deleteUserGroupRole(groupId);
        if (roleIds.length > 0){
            row += groupMapper.setUserGroupRole(groupId,roleIds);
        }
        if(row > 0){
            ZSetRedisHelper.updateSet(REDIS_VAL_NAME,REDIS_VAL_KEY,groupMapper.selectUserGroupById(groupId));
        }
        return row;
    }
}
